package com.peycash.monitoring.persistencia.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.peycash.monitoring.dominio.Sms;
import com.peycash.monitoring.persistencia.PersistenciaException;
import com.peycash.monitoring.persistencia.dao.SMSDAO;

@Repository
public class SMSDAOImpl extends BaseDAOImpl<Sms, BigDecimal> implements SMSDAO {

	@Override
	public BigDecimal count() throws PersistenciaException {
		try {
			Criteria criteria = getSessionFactory().getCurrentSession()
					.createCriteria(Sms.class)
					.setProjection(Projections.rowCount());
			BigDecimal bd = new BigDecimal(String.valueOf(criteria
					.uniqueResult()));
			return bd;
		} catch (Exception e) {
			throw new PersistenciaException("Error en count:" + e.getMessage(),
					e);
		}
	}

	@Override
	public List<Sms> findPendings() throws PersistenciaException{
		try {
			Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Sms.class);
			criteria.add(Restrictions.isNull("respServiceSms"));
			List<Sms> list = criteria.list();
			return list;
		} catch (Exception e) {
			throw new PersistenciaException("Error en findPendings:"
					+ e.getMessage(), e);
		}
	}
	
	@Override
	public List<Sms> findByNoCelEnvia(String noCel) throws PersistenciaException{
		try{
			Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Sms.class);
			criteria.add(Restrictions.like("numCelEnv", Long.parseLong(noCel)));
			List<Sms> list =  criteria.list();
			return list;
		}catch (Exception e) {
			throw new PersistenciaException("Error en findByNoCelEnvia:"
					+ e.getMessage(), e);
		}
	}
	

}
